from rest_framework import viewsets  # 响应
from rest_framework.decorators import action #行为路由器
from rest_framework.response import Response

from drfstudy.models import *
from drfstudy.serializers import *

'''
    视图集：封装了一套响应式方法
    定义：
        create创建数据，list查询所有，retrieve单挑查询，update修改，delete删除
'''


# Create your views here.
# class StudentViewSet(viewsets.ViewSet):
#     # 查询所有数据
#     def list(self, request):
#         students = Student.objects.all()
#         # 序列化： 查询机==>json
#         serializer = StudentSerializer(students, many=True)
#         return Response(serializer.data)
#
#     # 创建数据
#     def create(self, request):
#         # 反序列化：json==>模型类
#         serializer = StudentSerializer(data=request.data)  # 前端传过来的表单、json数据
#         # 校验是否符合模型数据
#         serializer.is_valid(raise_exception=True)
#         # 保存
#         serializer.save()
#         return Response(serializer.data)
#
#     # 查询单行数据
#     def retrieve(self, request, pk):  # pk参数，用户接收主键id的值
#         try:
#             student = Student.objects.get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=status.HTTP_404_NOT_FOUND)
#         # 模型==>json，前端 序列化
#         serializer = StudentSerializer(student)
#         return Response(serializer.data)
#
#     # 修改数据
#     def update(self, request, pk):
#         try:
#             student = Student.objects.get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=status.HTTP_404_NOT_FOUND)
#
#         # 反序列化：json==>模型类,2个参数是修改，1个参数是新增
#         serializer = StudentSerializer(student, data=request.data)  # 前端传过来的表单、json数据
#         # 校验是否符合模型数据
#         serializer.is_valid(raise_exception=True)
#         # 保存
#         serializer.save()
#         return Response(serializer.data)
#
#     # destory: 删除数据
#     def destory(self, request, pk):
#         try:
#             student = Student.objects.get(id=pk)
#         except Student.DoesNotExist:
#             return Response(status=status.HTTP_404_NOT_FOUND)
#         # 删除: 重写（逻辑删除）
#         student.delete()
#         return Response(status=status.HTTP_201_CREATED)

# Create your views here.
# 学生的视图集

class StudentViewSet(viewsets.ModelViewSet):
    queryset = Student.objects.filter(is_deleted=False)  # 逻辑删除为False的值，未被删除的值
    serializer_class = StudentSerializer  # 指定使用的序列化器


# 班级的视图集
class ClassesViewSet(viewsets.ModelViewSet):
    queryset = Classes.objects.filter(is_deleted=False)  # 逻辑删除为False的值，未被删除的值
    serializer_class = ClassesSerializer  # 指定使用的序列化器

    # 行为方法，自定义方法
    @action(methods=['get'],detail=False)
    def last(self, request):
        # 班级的最后一个数据
        classes = Classes.objects.last()
        # 序列化，查询集==>json
        return Response(self.get_serializer(classes).data)

    # detail=True,方法进行传参
    # @action(methods=['get'],detail=True)
    # def last(self, request,pk):
    #     # 班级的最后一个数据
    #     classes = Classes.objects.last()
    #     # 序列化，查询集==>json
    #     return Response(self.get_serializer(classes).data)
